class BlockModel {
    constructor(db, col_name, schema, logger) {
        this.model = db.model(col_name, schema);
        this.logger = logger;
    }
    async getDocByFilter(filter) {
        try{

            let ret = await  this.model.findOne(filter);
            return ret;
        }catch(e){
            this.logger.error("getDocByFilter e: ",e);
        }
    }
    async getDocsByFilter(filter,projection) {
        try{
            if(projection) {
                let ret = await  this.model.find(filter, projection);
                return ret;
            }else{
                return await this.model.find(filter);
            }

        }catch(e){
            this.logger.error("getDocsByFilter e: ",e);
        }

    }
    async insertBlock(blockId, block){
        let filter = {blockId:blockId};
        let oldDoc = await this.module.findOne(filter);
        if(oldDoc) {
            this.logger.debug('find old docs, no need to insert');
        }
        else{
            let doc = new this.model({blockId:blockId, block:block});
            let newDoc = await doc.save();
            if(newDoc) {
                this.logger.debug("insertTrade, newDoc: ", newDoc);
            }
        }
    }
}

module.exports = BlockModel;